<template>
  <div class = 'login_container'>
    <div class = 'login_box'>
      <div class = 'avatar_box'>
        <img src="../assets/logo.png" alt="">
      </div>
      <div>
        <!-- ref绑定实例对象，:model绑定数据对象 :rules绑定规则对象 -->
        <el-form class = 'login_form' ref = 'LoginFormRef' :model = 'loginform' :rules = 'LoginFromRules'>
          <!-- prop引用username输入规则 -->
          <el-form-item prop = 'username'>
            <!-- icon引入图标样式，外联css图标引用规则：样式名 样式图标名 -->
            <el-input prefix-icon = 'iconfont icon-user' v-model = 'loginform.username' placeholder="请输入用户名"></el-input>
          </el-form-item>
          <!-- prop引用password输入规则 -->
          <el-form-item prop = 'password'>
            <el-input prefix-icon = 'iconfont icon-3702mima' type = 'password' v-model = 'loginform.password' placeholder="请输入密码" @keyup.enter.native="Login"></el-input>
          </el-form-item>
          <el-form-item class = "btns">
            <el-button type="primary" @click="Login">登录</el-button>
            <!-- 绑定重置事件 -->
            <el-button type="info" @click = 'resetLoginForm'>重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      loginform: {
        username: '',
        password: ''
      },
      LoginFromRules: {
        // 用户名规则
        username: [
          // required为true表示不得为空
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符之间', trigger: 'blur' }
        ],
        // 密码规则
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6到15个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      // 重置到data中的默认值
      this.$refs.LoginFormRef.resetFields()
    },
    Login () {
      // 回调函数
      this.$refs.LoginFormRef.validate(async (valid) => {
        // 检查是否符合规则，返回true或者false
        if (!valid) {
          // 如果不符合规则
          return
        }
        // 发起请求
        const { data: res } = await this.$http.post('login', this.loginform)
        // 检查状态码
        if (res.meta.status !== 200) {
          return this.$message.error('登录失败！')
        }
        this.$message.success('登录成功！')
        // 把token存入session域
        window.sessionStorage.setItem('token', res.data.token)
        // 编程路由重定向
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped>
.login_container{
  background-color:#2b4b6b ;
  height: 100%;
}
.btns{
  display: flex;
  justify-content: flex-end;
}
.login_box{
  width: 450px;
  height: 300px;
  background-color: aliceblue;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top:50%;
  transform: translate(-50%, -50%);
}
.avatar_box{
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 5px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}
.avatar_box img{
    width: 100%;
    height:100%;
    border-radius: 50%;
    background-color: #eee;
  }
.login_form{
  position: absolute;
  bottom: 0;
  width:100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
